<script lang="ts" setup>
import { ref, nextTick } from 'vue'
import { ElMessage, ElTree } from 'element-plus'
import yspglService from '@/api/yspgl'
import TreeUtilDTO from '@/utils/tree'
import { useRoute } from 'vue-router'
const emit = defineEmits(['nodeClick'])
const route = useRoute()

// 入参
const filterForm = ref({
  gjzValue: '',
})

// 生命周期
// onMounted(() => {
//   // 查询树
//   queryTree()
// })

const init = () => {
  // 查询树
  queryTree()
}

const treeData = ref<any>([])
const treeRef = ref<InstanceType<typeof ElTree>>()
const firtsNode = ref<any>()
/*
 * 查询树
 */
const queryTree = () => {
  //处理数据
  const params = {
    bmsah: route.query.bmsah as string,
  }
  yspglService
    .querySxmlsByBmsah(params)
    .then((res) => {
      treeData.value = TreeUtilDTO.getTreeData(res.data, 'bh', 'fbh')
      treeData.value?.forEach((data: any) => {
        if (data.children && data.children.length > 0) {
          data.children.forEach((data1: any) => {
            data.wjsl += data1.wjsl
          })
        }
      })

      if (treeData.value.length) {
        firtsNode.value = null
        // 递归选择第一个
        getFirstNode(treeData.value)
        if (firtsNode.value) {
          handleCheckNode(firtsNode.value)
        }
      }
    })
    .catch((err) => {
      !err.isError && ElMessage({ type: 'warning', message: err.message })
    })
}

const getFirstNode = (arr: any[]) => {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].children && arr[i].children.length > 0) {
      getFirstNode(arr[i].children)
    } else {
      firtsNode.value = arr[i]
    }
    return
  }
}

const handleCheckNode = (node: any) => {
  treeNodeClick(node, true)
  nextTick(() => {
    treeRef.value!.setCurrentKey(node.bh)
  })
}

// 文字过滤
const filterTree = () => {
  treeRef.value?.filter(filterForm.value.gjzValue)
}

/**
 * 树节点过滤
 */
const filterNode = (value: any, data: any) => {
  let flag = true
  if (value) {
    if (typeof value === 'string') {
      // 文字过滤
      flag = data[defaultProps.label].indexOf(value) !== -1
    }
  }
  return flag
}

/*
 * 树节点点击（抛出方法到主界面）
 */
const treeNodeData = ref()

const treeNodeClick = (data: any, flag: any) => {
  if (flag !== true) {
    window.parent.postMessage('triggerEvent', route.query.url as string)
  }
  treeNodeData.value = data
  emit('nodeClick', treeNodeData.value)
}

const defaultProps = {
  children: 'children',
  label: 'mc',
}

defineExpose({ init })
</script>
<template>
  <div class="yx-tree-container">
    <div class="center">
      <el-input
        v-model="filterForm.gjzValue"
        autosize=""
        placeholder="请输入查询关键字"
        @change="filterTree"
      >
        <template #suffix>
          <el-icon @click="filterTree" class="el-input__icon"><Search /></el-icon>
        </template>
      </el-input>
    </div>
    <div class="bottom">
      <el-tree
        ref="treeRef"
        style="max-width: 600px"
        :data="treeData"
        node-key="bh"
        highlight-current
        default-expand-all
        :expand-on-click-node="false"
        :props="defaultProps"
        @node-click="treeNodeClick"
        :filter-node-method="filterNode"
      >
        <template #default="{ data }">
          <div class="custom-tree-node">
            <img v-if="data.cj === 1" src="/src/assets/images/file-icon.png" alt="" />
            <img
              v-if="data.mllxmc === '视频' && data.sfxswj === 'Y'"
              src="/src/assets/images/xssptb.png"
              alt=""
            />
            <img
              v-else-if="data.mllxmc === '视频' && (!data.sfxswj || data.sfxswj === 'N')"
              src="/src/assets/images/xxsptb.png"
              alt=""
            />
            <img
              v-else-if="data.mllxmc === '音频' && data.sfxswj === 'Y'"
              src="/src/assets/images/xsyptb.png"
              alt=""
            />
            <img
              v-else-if="data.mllxmc === '音频' && (!data.sfxswj || data.sfxswj === 'N')"
              src="/src/assets/images/xxyptb.png"
              alt=""
            />
            <img
              v-else-if="data.mllxmc === '照片' && data.sfxswj === 'Y'"
              src="/src/assets/images/xszptb.png"
              alt=""
            />
            <img
              v-else-if="data.mllxmc === '照片' && (!data.sfxswj || data.sfxswj === 'N')"
              src="/src/assets/images/xxzptb.png"
              alt=""
            />
            <span :class="data.cj === 2 ? 'over-title' : ''" :title="data.mc"
              >&nbsp;{{ data.mc }}&nbsp;</span
            >
            <span>({{ data.wjsl }})</span>
          </div>
        </template>
      </el-tree>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.yx-tree-container {
  width: 100%;
  height: calc(100% - 44px);
  .top {
    margin-bottom: 10px;
  }
  .center {
    padding-bottom: 10px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.15);
    .el-input {
      :deep(.el-input__wrapper) {
        background-color: transparent;
      }
    }
  }
  .bottom {
    height: calc(100% - 43px);
    overflow-y: auto;
    .el-tree {
      background-color: transparent;
      &.el-tree--highlight-current {
        :deep(.el-tree-node.is-current) > .el-tree-node__content {
          background-color: #d7e5f9;
        }
      }
    }
  }
}
.add-ml {
  width: 100%;
}

.el-input__icon {
  cursor: pointer;
}

.custom-tree-node {
  width: calc(100% - 40px);
  display: flex;
  align-items: center;
  .over-title {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}
:deep(.el-tabs .el-tabs__content) {
  height: calc(100% - 40px);
  /* overflow: auto; */
  padding: 0;
}

.el-button + .el-button {
  margin-left: 0;
}
</style>
